<template>
	<view>
		<uni-popup ref="replacePop">
			<view class="pop">
				<view class="top">
					<text class="top-title">盘盈物料信息</text>
					<icon class="close-btn" type="clear" size="26" @click="close" />
				</view>
				<view>
					<uni-search-bar @confirm="search" :focus="true"
					v-model="searchValue"
									@cancel="cancel" @clear="clear">
					</uni-search-bar>
				</view>
			</view>
			<view class="uni-form-list">
				<scroll-view scroll-y="true" class="scroll">
					<view class="" v-for="(item,index) in dataList" :key = "index">
						<view class="panel" >
							<view class="panel_title">
								<view class="">{{item.fmaterialName}}</view>
							</view>
							<view class="panel_content">
								<view class="panel_content_row">
									<view class="panel_content_row_item">
										<view class="panel_content_row_item_title">编码：</view>
										<view class="panel_content_row_item_value">{{ item.fmaterialNumber }}</view>
									</view>
								</view>
							</view>
							<view class="panel_content">
								<view class="panel_content_row">
									<view class="panel_content_row_item">
										<view class="panel_content_row_item_title">规格：</view>
										<view class="panel_content_row_item_value">{{ item.fspecification }}</view>
									</view>
									<!-- 暂时没有材质，用单位代替 -->
									<view class="panel_content_row_item">
										<view class="panel_content_row_item_title">单位：</view>
										<view class="panel_content_row_item_value">{{ item.fbaseUintName }}</view>
									</view>
								</view>
							</view>
							<view class="panel_content">
								<view class="panel_content_row">
									<view class="panel_content_row_item">
										<view class="panel_content_row_item_title">盘盈数量：</view>
					
										<view class="panel_content_row_item_value_input">
											<input type="number" placeholder="请输入数量"
											v-model="item.replaceCount"
											@input="inputClick(item)"
											placeholder-class="input-placeholder">
										</view>
									</view>
					
								</view>
							</view>
							<view class="flex-list-btn" >
								<view class="flex-list-btn-item" @click="addReplace(item)">
									添加
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import {getMaterial,addProfitMaterial} from "@/utils/api.js"
	export default {
		components: {

		},
		emits: [],
		props: {
			hiddenDelete: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				type: null,
				searchValue : '',
				title: '',
				billNo : '',
				stockNo : '',
				locationNo :'',
				stockName : '',
				type: 'center',
				dataList: [],
				materialNo : '',
				FPRDORGID : '',
				count : 0,//应发数量
				commedList :[]//推荐仓位列表

			}
		},
		computed: {

		},
		methods: {
			/**
			 * 输入框校验只允许是正整数
			 */
			inputClick(e){
				const v = e.replaceCount
				const zero = /^(0{2,})|[^0-9]/g
				let final = 0
				if (!v) {
				  final = 0
				} else {
				  final = v.toString().replace(zero, (v) => {
					return 0
				  })
						
				  if (final.split('')[0] * 1 === 0) {
					final = final.slice(1) - 0 || 0
				  }
				}
				this.$nextTick(() => {
				 e.replaceCount = final.toString() || '0'
				})
			},
			//添加盘盈物料
			addReplace(item){
			
				if (item.replaceCount == 0){
					uni.showToast({
						title:'数量不能是0',
						icon:"error"
					})
					return;
				}
				
				uni.showModal({
					title: "提示",
					content: "是否确认添加",
					success: (result) => {
						if (result.confirm) {
							try{
								addProfitMaterial({billNo : this.billNo,
								materialNo : item.fmaterialNumber,
								FPRDORGID : this.FPRDORGID,
								count : item.replaceCount,
								stockNo : this.stockNo,
								stockName : this.stockName,
								locationNo : this.locationNo}).then(res =>{
									if (res.data.status == 200){
										uni.showToast({
											title:'添加成功',
											icon:'none'
										})
										setTimeout(() =>{
											this.$emit('success','')
											this.close()
										},1000)
									}
								})
							} catch{
								console.log(res)

							} finally{

							}

						}
					}
				})
			},
			/**
			 * @param {Object} res查询替代料
			 */
			search(res){
				let self = this;
				getMaterial({materialNo : this.searchValue,FPRDORGID:this.FPRDORGID}).then(res => {
					if (res.data.status == 200){
						self.dataList = res.data.data.dataList
					}
				})
			},
			/**
			 * 开启
			 */
			open(data) {
				
				this.billNo = data.billNo
				this.FPRDORGID = data.FPRDORGID
				this.stockName = data.stockName
				this.stockNo = data.stockNo
				this.locationNo = data.locationNo
				this.$refs.replacePop.open()
			},


			/**
			 * 关闭画面
			 */
			close() {
				this.$refs.replacePop.close()
			},

		}
	}
</script>

<style lang="scss" scoped>
	@import "../../static/css/common";
	.pop {
		display: flex;
		flex-direction: column;
		padding-top: 200px;
		background-color: white;
		width:400px;
	}

	.top {
		height: 20px;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		border-bottom: 1px solid #F1F1F1;
		padding-left: 10px;
		padding-bottom: 10px;
		position: relative;
	}

	.top-title {
		display: flex;
		flex-direction: column;
		font-weight: bold;
		margin-bottom: 10px;
	}

	.close-btn {
		display: flex;
		flex-direction: column;
		position: absolute;
		right: 20px;
	}
	.uni-form-list{
		display: flex;
		height: 500px;
	}

	.font {
		color: $font-hc;
	}


</style>